#include <reg52.h>
//ADC0809 模数转换显示
#define uchar unsigned char
#define uint unsigned int
uchar code LEDData[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
sbit OE = P1 ^ 0;
sbit EOC = P1 ^ 1;
sbit ST = P1 ^ 2;
sbit CLK = P1 ^ 3;
void DelayMS(uint ms)
{
  uchar i;
  while (ms--)
    for (i = 0; i < 112; i++)
      ;
}
void sendStr(uchar *s)
{
  uint i = 0;
  while (s[i] != '\0')
  {
    SBUF = s[i] + '0';
    i++;
    DelayMS(10);
  }
}
char Display_Result(uchar m)
{
  uchar ch[4];
  uint d = m;
  /**********根据当前输入量m大小 ，在【0-255】所占比例，转换为当前数字量 **********/
  float k = d * 1.00;
  k = (k / 256 * 5 * 1000); //1000代表当前所设定电阻的值。256代表整体【0-255】的长度
  d = k;
  /*****P0口分别显示个、十、百、千等位的大小（段码），P2显示的位置（位码）***********/
  P3 = 0x7f; //个 01111111
  P0 = LEDData[d % 10];
  DelayMS(1);
  P3 = 0xbf; //十 10111111
  P0 = LEDData[d / 10 % 10];
  DelayMS(1);
  P3 = 0xdf; //百 11011111
  P0 = LEDData[d / 100 % 10];
  DelayMS(1);
  P3 = 0xef; //千 11101111
  P0 = LEDData[d / 1000] | 0x80;
  DelayMS(1);
  ch[3] = d % 10;
  ch[2] = d / 10 % 10;
  ch[1] = d / 100 % 10;
  ch[0] = d / 1000;
  return ch;
}
void adcInit(void)
{
  TMOD = 0x02;
  TH0 = 0x14;
  TL0 = 0x00;
  // TH0 = 246; //定时器初值高8位
  // TL0 = 246; //定时器初值低8位
  EA = 1;
  ET0 = 1; //开定时器中断
  TR0 = 1;
}
void uartInit(void)
{
  TMOD = 0X20;
  SCON = 0x50; //工作方式1，波特率9600bit/s,允许接收
  TL1 = 0XFD;
  TH1 = 0XFD;
  TR1 = 1; //定时器1
  ES = 1;  //开中断.
  EA = 1;
}
void main()
{
  char d[4];
  adcInit();
  uartInit();
  while (1)
  {
    ST = 0;
    ST = 1;
    ST = 0;
    while (EOC == 0)
      ;
    OE = 1;
    *d = Display_Result(P2);
    sendStr(d);
    DelayMS(1000);
    OE = 0;
  }
}
void Timer0_INT() interrupt 1
{
  CLK = ~CLK;
}
void uart(void) interrupt 4
{
  uchar temp;
  if (RI)
  {
    RI = 0;
    temp = SBUF;
    SBUF = temp;
  }
  else
  {
    TI = 0;
  }
}